package com.spark.service;

import com.spark.entity.User;
import com.spark.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @Author sc
 * @DATE 2024/5/23 10:32
 * @Description: redis缓存测试
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
public class RedisDemoService {

    private final RedisUtils redisUtils;

    @Cacheable(value = "user:cache", key = "#id")
    public Object getData(String id) {
        // 从数据库或其他数据源获取数据
        User user = new User();
        user.setId(id);
        user.setUserName("TEST" + id);
        user.setPwd("123" + id);
        return user;
    }

    @CachePut(value = "user:cache", key = "#id")
    public Object updateData(String id) {
        // 更新数据库或其他数据源的数据
        User user = new User();
        user.setId(id);
        user.setUserName("TEST-XX" + id);
        user.setPwd("123-XX" + id);
        return user;
    }

    @CacheEvict(value = "user:cache", key = "#id")
    public void deleteData(String id) {
        // 删除数据库或其他数据源的数据
    }

}
